/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Klassen;

import java.util.ArrayList;

/**
 *
 * @author Serenity
 */
public abstract class FieldContainer {
    protected Field[] fields;
    protected ArrayList<Integer> missing_values;
    protected boolean solved = false;
    protected int position;
    protected Board board;
    
    public FieldContainer(Board board, int position) {
        this.missing_values = new ArrayList<Integer>();
        this.solved = false;
        this.board = board;
        this.position = position;
        this.fields = new Field[9];
    }
    
    public boolean remove_missing_value(Integer remove) {
        if(this.missing_values.remove(remove)) {
            if(this.missing_values.isEmpty()) {
                this.solved = true;
            }
            return true;
        } else {
            return false;           // Fehler ist aufgetreten???
        }
    }
    
    public boolean generate_missing_values() {
        if(!this.check_for_error()) {
            return false;
        } else {
            this.missing_values.clear();
            ArrayList<Integer> given_values = new ArrayList<Integer>();
            for(int i=0; i<9; i++) {
                if(this.fields[i].getValue() !=0) {
                    given_values.add(new Integer(this.fields[i].getValue()));
                }
            }
            for(int v=1; v<=9; v++) {
                Integer suggestion = new Integer(v);
                if(!given_values.contains(suggestion)) {
                    this.missing_values.add(suggestion);
                }
            }
            if(this.missing_values.isEmpty()) {
                this.solved = true;
            } else {
                this.solved = false;
            }
            return true;
        }
    }
    
    private boolean check_for_error() {
        boolean error = false;
        ArrayList<Integer> values = new ArrayList<Integer>();
        for(int i=0; i<9; i++) {
            Integer value = new Integer(this.fields[i].getValue());
            if(value != 0 && values.contains(value)) {
                error = true;
            } else if(value != 0) {
                values.add(value);
            }
        }
        if(error) {
            System.out.println("Fehler");
        }
        return !error;
    }
    
    public void print_suggestions() {
        if(this.missing_values.isEmpty()) {
            System.out.println("isEmpty");
        }
        else {
            StringBuilder sb = new StringBuilder();
            sb.append(this.missing_values.get(0));   
            for(int i=1; i<this.missing_values.size(); i++) {
                sb.append(" ,");
                sb.append(this.missing_values.get(i));
            }
            System.out.println(sb);
        }
    }
    
    public ArrayList<Field> fields_for_stepTwo(int range) {
        ArrayList<Field> fields_stepTwo = new ArrayList<Field>();
        for(int i=0; i<9; i++) {
            if(this.fields[i].getValue()==0 && this.fields[i].getPossible_values().size() == range) {
                fields_stepTwo.add(this.fields[i]);
            }
        }
        return fields_stepTwo;
    }

    public Field[] getFields() {
        return fields;
    }

    public int getPosition() {
        return position;
    }

    public boolean isSolved() {
        return solved;
    }

    public ArrayList<Integer> getMissing_values() {
        return missing_values;
    }
    
    
    
}
